//
//  XXBaseTabBarController.swift
//  APP
//
//  Created by Rdxer on 2017/12/6.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit
import Async

public protocol TabBarChildrenViewController:NSObjectProtocol{
    func tabBarChildren(didSelect:Bool)
}

extension XXBaseNavigationController:TabBarChildrenViewController{
    public func tabBarChildren(didSelect: Bool) {
        viewControllers.forEach { index, vc in
            if let vc = vc as? TabBarChildrenViewController{
                vc.tabBarChildren(didSelect: didSelect)
            }else{
            }
        }
    }
}


@objc
@objcMembers
open class XXBaseTabBarController: UITabBarController,UITabBarControllerDelegate {
    
    deinit {
        XXLoger.w(self.classForCoder.description() + "已释放" )
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        Async.main {
            XXLoger.w("已创建" + self.classForCoder.description() )
        }
    }
    
    weak var lastSelectViewController:TabBarChildrenViewController? = nil
    open func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        if lastSelectViewController == nil{
            
            self.viewControllers?.filter({$0 != viewController}).forEach({ i, vc in
                if let vc = vc as? TabBarChildrenViewController{
                    vc.tabBarChildren(didSelect: false)
                }
            })
        }else{
            lastSelectViewController?.tabBarChildren(didSelect: false)
        }
                
        if let vc = viewController as? TabBarChildrenViewController{
            lastSelectViewController = vc
            vc.tabBarChildren(didSelect: true)
        }else{
            lastSelectViewController = nil
        }
    }

}
